package org.runtimedao.template;

import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public class DAOTemplate {

	private static VelocityEngine engine;
	private VelocityContext context;
	private Template template;

	static {
		engine = new VelocityEngine();
		engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
		engine.setProperty("classpath.resource.loader.class",
				ClasspathResourceLoader.class.getName());
		engine.init();
	}

	private String fileName;

	public DAOTemplate(String fileName) {
		this.fileName = fileName;
		this.context = new VelocityContext();
		this.template = engine.getTemplate(fileName);
	}

	public void setParameter(String param, Object value) {
		context.put(param, value);
	}

	public String merge() {
		StringWriter writer = new StringWriter();
		template.merge(context, writer);
		return writer.toString();
	}

	public String getFileName() {
		return fileName;
	}
}
